// 获取某天所在的周的起始日期和结束日期
export function getWeekStartAndEnd (val) {
  const now = val ? new Date(val) : new Date()

  return {
    startDate: formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() - (now.getDay() - 1))),
    endDate: formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() + (6 - (now.getDay() - 1))))
  }
}

// 获取某天所在的月的起始日期和结束日期
export function getMonthStartAndEnd (val) {
  const now = val ? new Date(val) : new Date()

  return {
    startDate: formatDate(new Date(now.setDate(1))),
    endDate: formatDate(new Date(new Date(now.getFullYear(), now.getMonth() + 1, 0)))
  }
}

// 获取某天所在的年的起始日期和结束日期
export function getYearStartAndEnd (val) {
  const startDate = val ? new Date(val) : new Date()
  const endDate = val ? new Date(val) : new Date()

  startDate.setMonth(0)
  startDate.setDate(1)

  endDate.setMonth(12)
  endDate.setDate(0)
  return {
    startDate: formatDate(new Date(startDate)),
    endDate: formatDate(new Date(endDate))
  }
}

// 日期格式化
export function formatDate (date) {
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }

  if (day < 10) {
    day = '0' + day
  }

  return year + '-' + month + '-' + day
}
